﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhotoViewerLib
{
    // Util is an helper class. All libraries methods are called from this class which interacts with the database
    public class Util
    {
        public static List<Photo> getListPhotos()
        {
            return Database.getListPhotos();
        }

        public static List<Category> getListCategory()
        {
            return Database.getListCategory();
        }

        public static List<Photo> getPhotosForCategory(Category c)
        {
            return Database.getPhotosForCategory(c);
        }

        public static List<Tag> getTagsForPhoto(Photo p)
        {
            return Database.getTagsForPhoto(p);
        }

        public static void addPhoto(Photo p)
        {
            Database.addPhoto(p.cid, p.filePath, p.rating);
        }

        public static void addCategory(Category c)
        {
            Database.addCategorie(c.name);
        }

        public static void editPhoto(int photo_id, Photo newPhoto)
        {
            Database.editPhoto(photo_id, newPhoto.cid, newPhoto.filePath, newPhoto.rating);
        }

        public static void editCategory(int categ_id, Category newCateg)
        {
            Database.editCategorie(categ_id, newCateg.name);
            foreach (Photo p in newCateg.photos)
            {
                p.cid = categ_id;
                Util.editPhoto(p.uid, p);
            }
        }

        public static void delPhoto(int photo_id)
        {
            Database.delPhoto(photo_id);
        }

        public static void addTag(List<Tag> tags)
        {
            foreach (Tag t in tags)
            {
                Database.addTag(t.pid, t.title);
            }
        }

        public static string getDatabaseFilePath()
        {
            return Database.getDbFilePath();
        }

        public static void setDbFilePath(string filePath)
        {
            Database.setDbFilePath(filePath);
        }

        public static void clearDB()
        {
            Database.delAllCateg();
            Database.delAllPhoto();
        }


        public static Category getCategoryById(string p)
        {
            foreach (Category c in Util.getListCategory())
            {
                if (c.uid.ToString().CompareTo(p) == 0)
                    return c;
            }
            return null;
        }

        public static Photo getPhotoById(Guid picIdentifier)
        {
            foreach (Photo p in Util.getListPhotos())
            {
                if (p.uid == picIdentifier)
                    return p;
            }
        }
    }
}
